package com.hzit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hzit.controller.StudentController;
import com.hzit.entity.Classes;
import com.hzit.entity.PageResult;
import com.hzit.entity.Student;
import com.hzit.entity.StudentExample;
import com.hzit.mapper.ClassesMapper;
import com.hzit.mapper.StudentMapper;
import com.hzit.service.StudentService;
import com.hzit.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Service
public class StudentServiceImpl implements StudentService {
	@Autowired
	private StudentMapper studentMapper;
	@Autowired
	private ClassesMapper classesMapper;


	//1. 查询所有学生
	@Override
	public List<StudentVo> findAll() {
		//1.1 查询所有学生
		List<Student> students = studentMapper.selectByExample(null);
		//1.2 将上面的学生集合转换为StudetnVo类型的集合
		List<StudentVo> collect = getStudentVos(students);

		return collect;
	}

	//2. 分页查询
	@Override
	public PageResult<StudentVo> findByPage(Integer page, Integer pageSize) {
		//2.1 开始分页
		PageHelper.startPage(page,pageSize);
		//2.2 开始查询
		StudentExample example = new StudentExample();
		List<Student> students = studentMapper.selectByExample(example);
		//2.3 将上面的分而的结果集转换为Page<Student>对象
		Page<Student> studentPage = (Page<Student>) students;
		//2.4 得到分页的结果
		List<Student> result = studentPage.getResult();
		//2.5 将上面的集合转换为List<StudentVo>对象
		List<StudentVo> collect = getStudentVos(result);
		//2.6 构造PageResult<StudentVo>对象
		PageResult<StudentVo> pr = new PageResult<>(studentPage.getTotal(),collect);
		//2.7 返回
		return pr;
	}

	//3. 条件查询带分页
	@Override
	public PageResult<StudentVo> search(Integer page, Integer pageSize, StudentVo vo) {
		//3.1 开始分页
		PageHelper.startPage(page,pageSize);
		//3.2 开始条件查询
		StudentExample example = new StudentExample();
		StudentExample.Criteria criteria = example.createCriteria();
		if(vo != null){
			if(StrUtil.isNotBlank(vo.getSname())) {
				criteria.andSnameLike("%" + vo.getSname() + "%");
			}
			if(StrUtil.isNotBlank(vo.getAddr())){
				criteria.andAddrLike("%" + vo.getAddr() + "%");
			}
			if(vo.getCid() != null && vo.getCid() != 0){
				criteria.andCidEqualTo(vo.getCid());
			}
		}
		//3.3 进行条件查询
		List<Student> students = studentMapper.selectByExample(example);
		//3.4 将上面的集合转换为Page<Student>对象
		Page<Student> studentPage = (Page<Student>) students;
		//3.5 得到分页结果集
		List<Student> result = studentPage.getResult();
		//3.6 将上面的分页结果集转换为 List<StudentVo>对象
		List<StudentVo> studentVos = getStudentVos(result);
		//3.7 返回分页结果集
		return new PageResult<>(studentPage.getTotal(),studentVos);
	}

	//4. 添加学生
	@Override
	public void add(StudentVo vo) {
		//4.1 将StudentVo转换为Student对象
		Student student = new Student();
		BeanUtil.copyProperties(vo,student,true);
		//4.2 转换完成后就添加到数据库中
		studentMapper.insert(student);
	}

	//5. 修改学生
	@Override
	public void update(StudentVo vo) {
		//5.1 将vo对象转换为student对象
		Student student = new Student();
		BeanUtil.copyProperties(vo,student,true);
		//5.2 执行修改操作
		studentMapper.updateByPrimaryKey(student);
	}
	//6. 批量删除学生
	@Override
	public void delete(Integer[] sids) {
		// for (Integer sid : sids) {
		// 	studentMapper.deleteByPrimaryKey(sid);
		// }
		//可以如下删除（方法二）,效率更高！
		StudentExample example = new StudentExample();
		StudentExample.Criteria criteria = example.createCriteria();
		criteria.andSidIn(Arrays.asList(sids));
		studentMapper.deleteByExample(example);
	}

	// 将List<Student>集合转换为List<StudentVo>集合并返回
	private List<StudentVo> getStudentVos(List<Student> result) {
		return result.stream().map(m -> {
			//1.2.1 定义目标对象
			StudentVo vo = new StudentVo();
			//1.2.2 拷贝源对象到目标对象中
			BeanUtil.copyProperties(m, vo, true);
			//1.2.3 查询学生对应的班级名称
			Classes classes = classesMapper.selectByPrimaryKey(m.getCid());
			vo.setCname(classes.getCname());
			return vo;
		}).collect(Collectors.toList());
	}
}
